<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<button type="button" onclick="send_msg()"> 发送消息</button>
</body>
<script>
    var  index=0;
    if ("WebSocket" in window) {
        var wsServer = 'ws://192.168.56.100:9502';
        var ws = new WebSocket(wsServer);
        ws.onopen = function (evt) {
            if (ws.readyState == 1) {
                console.log('WebSocket 连接成功...');
            } else {
                console.log('WebSocket 连接失败...');
            }
        };
        ws.onmessage = function (evt) {
            console.log('接收到的数据: ' + evt.data+'->当前时间:'+get_time());
        };

        ws.onerror = function (evt) {
            alert('WebSocket 发生错误');
            console.log(evt);
        };

        ws.onclose = function() {
            alert('WebSocket 连接关闭');
            console.log('WebSocket 连接关闭...');
        };
    }else {
        alert("您的浏览器不支持 WebSocket!");
    }
    function send_msg(){
        index+=1;
        for(i=0;i<10000;i++){
            ws.send("你好啊 服务器 当前时间:"+get_time()+'->'+index.toString()+':'+i.toString());
        }
    }

    function get_time(){
//定义时间变量对象mydate
        var mydate=new Date();
        var hour=mydate.getHours();
        var minute=mydate.getMinutes();
        var seconds=mydate.getSeconds();
        return hour+":"+minute+":"+seconds;
    }


</script>
</html>